/*
 * Copyright (c) 2010 CCX(China) Co.,Ltd. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of
 * CCX(China) Co.,Ltd. ("Confidential Information").
 * It may not be copied or reproduced in any manner without the express 
 * written permission of CCX(China) Co.,Ltd.
 *
 * @author luosisi
 * Date: 2010-7-5 上午11:03:18
 */
package com.ccxe.manage.dao.impl;

import java.sql.ResultSet;

import com.ccxe.manage.dao.UserPasswordDAO;
import com.ccxe.manage.db.CcxDB;
import com.ccxe.manage.db.DBFactory;
import com.ccxe.manage.model.UserPasswordBean;

/**
 * @author luosisi
 *
 */
public class UserPasswordDaoImpl implements UserPasswordDAO {

	/**
	 * 修改密码
	 */
	public int UpdateUserPwd(UserPasswordBean up) {
		CcxDB ccxDB = null;
		int rs = 0;
		try {
			if (up != null) {
				ccxDB = DBFactory.getInstance();
				String sqlKey="update C_user_password set PASSWORD=?,PWD_FORCED=? where USER_ID=?";
				Object[] items = new Object[3];
				items[0]=up.getPassword();
				items[1]=up.getUserID();
				items[2]=up.getPwdForced();

				rs = ccxDB.executeUpdate(sqlKey, items);//执行操作
			}
		}catch (Exception e) {
			rs = 0;
			e.printStackTrace();
		} finally {
			try {
				if (ccxDB != null)
					ccxDB.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return rs;
	}

	/**
	 * 增加用户密码
	 *@author luosisi
	 * @param 用户ID
	 * @param 公司ID
	 * @param 用户密码
	 */
	public int addUserPwd(UserPasswordBean up) {
		CcxDB ccxDB = null;
		int rs = 0;
		try {
			if (up != null) {
				ccxDB = DBFactory.getInstance();
				String sqlKey="insert into C_user_password(USER_ID,PASSWORD,PWD_FORCED)" +
				" values(?,?,?)";
				Object[] items = new Object[3];
				items[0]=up.getUserID();
				items[1]=up.getPassword();
				items[2]=up.getPwdForced();
				rs = ccxDB.executeUpdate(sqlKey, items);//执行操作
			}
		}catch (Exception e) {
			rs = 0;
			e.printStackTrace();
		} finally {
			try {
				if (ccxDB != null)
					ccxDB.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return rs;
	}

	/**
	 * 删除密码(删除前需删除用户)
	 */
	public int deleteUserPwd(String uid, String cid) {
		CcxDB ccxDB = null;
		int rs = 0;
		try {
			if (cid != null && !cid.equals("") && uid != null
					&& !uid.equals("")) {
				ccxDB = DBFactory.getInstance();
				String sqlKey = "delete from C_user_password where USER_ID=? ";
				Object[] items ={uid};
				rs = ccxDB.executeUpdate(sqlKey, items);
			}
		} catch (Exception e) {
			rs = 0;
			e.printStackTrace();
		} finally {
			try {
				if (ccxDB != null)
					ccxDB.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return rs;
	}
	
	/**
	 * 根据用户ID获得该用户密码
	 */
	public UserPasswordBean getByIds(String uid, String cid) {
		UserPasswordBean upb=null;
		CcxDB ccxDB = null;
		ResultSet rs = null;
		try {
				if (cid != null && !cid.equals("") && uid != null&& !uid.equals("")) {
					ccxDB=DBFactory.getInstance();
					String sqlKey="select * from C_user_password where  USER_ID=?";
					Object[] items={uid};
					rs=ccxDB.executeQuery(sqlKey, items);
					if(rs.next()){
						upb=new UserPasswordBean();
						upb.setUserID(uid);
						upb.setPassword(rs.getString("PASSWORD"));
						upb.setPwdForced(rs.getString("PWD_FORCED"));
					}
				}
			}catch (Exception e) {
				upb=null;
				e.printStackTrace();
			} finally{
				try {
					if (ccxDB != null)	
						ccxDB.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			return upb;
	}


}
